package com.example.project1.main;

import javax.microedition.khronos.opengles.GL10;

import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.example.project1.GameActivity;
import com.example.project1.GameListener;
import com.example.project1.sandbox.Simulation;
import com.example.project1.screen.GameLoop;
import com.example.project1.screen.GameOver;
import com.example.project1.screen.GameScreen;
import com.example.project1.screen.StartScreen;
import com.scoreninja.adapter.ScoreNinjaAdapter;

/**
 * 
 * @author Ballach setup the gamesimulations implements lifecycle handles
 *         screens
 * 
 *         start with StartScreen isDone() returned switch to GameLoop if the
 *         game over switch to GameOverScreen isDone() returned switch to
 *         StartScreen
 */
public class SpaceInvaders extends GameActivity implements GameListener {

	GameScreen screen;
	Simulation simulation = null;

	public void onCreate(Bundle bundle) {
		setRequestedOrientation(0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		super.onCreate(bundle);
		setGameListener(this);

		if (bundle != null && bundle.containsKey("simulation"))
			simulation = (Simulation) bundle.getSerializable("simulation");

		Log.d("Space Invaders", "created, simulation: " + (simulation != null));
	}

	@Override
	public void onPause() {
		super.onPause();
		if (screen != null)
			screen.dispose();
		if (screen instanceof GameLoop)
			simulation = ((GameLoop) screen).simulation;
		Log.d("Space Invaders", "paused");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d("Space Invaders", "resumed");
	}

	@Override
	public void setup(GameActivity activity, GL10 gl) {
		if (simulation != null) {
			screen = new GameLoop(gl, activity, simulation);
			simulation = null;
			Log.d("Space Invaders", "resuming previous game");
		} else {
			screen = new StartScreen(gl, activity);
			Log.d("Space Invaders", "starting a new game");
		}
	}

	long start = System.nanoTime();
	int frames = 0;

	/**
	 * renders screen s
	 * witch to next screen and release resources from previous screen
	 * createes instances of startscreen/gameloop/gameoverscreen
	 */
	@Override
	public void mainLoopIteration(GameActivity activity, GL10 gl) {
		screen.update(activity);
		screen.render(gl, activity);

		if (screen.isDone()) {
			screen.dispose();
			Log.d("Space Invaders", "switching screen: " + screen);
			if (screen instanceof StartScreen)
				screen = new GameLoop(gl, activity);
			else if (screen instanceof GameLoop)
				screen = new GameOver(gl, activity,
						((GameLoop) screen).simulation.score);
			else if (screen instanceof GameOver)
				screen = new StartScreen(gl, activity);
			Log.d("Space Invaders", "switched to screen: " + screen);
		}

		frames++;
		if (System.nanoTime() - start > 1000000000) {
			Log.d("Space Invaders", "fps: " + frames);
			frames = 0;
			start = System.nanoTime();
		}
	}

}
